<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>lesson 4</title>
    <style>
        span {
            margin-right: 15px;
        }
    </style>
    <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="root">

    </div>
</body>
<!-- 第一章 vue初探/1-5组件概念初探，对Todolist进行组件拆分 -->
</html>
<script>
    const app = Vue.createApp({
        data() {
            return {
                value: "",
                List: [],
            };
        },
        methods: {
            // 增加
            handleAddItem() {
                // 非空判断
                if (this.value === "") {
                    alert("内容不能为空");
                    return false;
                } else {
                    // 添加内容
                    this.List.push(this.value);
                    // 清空输入框
                    this.value = ""
                }
            },
        },
        template:
            `  
            <div>
                <input placeholder="请输入内容"
                    v-model="value" 
                    @keyup.enter="handleAddItem" />  
                <button
                    v-on:click="handleAddItem" 
                    v-bind:title="value"
                 >
                    增加
                </button>
                 <ul>
                    <todo-item 
                        v-for="(item,index) in List" 
                        v-bind:content="item" 
                        v-bind:index="index"    
                    />
                 </ul>    
            </div>
            `
    })


    app.component('todo-item', {
        props: ["content", "index"],
        template: '<li>{{index}}  --  {{content}}</li>'
    });

    app.mount('#root')
</script>